#!/bin/bash
#

echo "start building..."

export ANDROID_SDK="/media/c/Dev/Android/sdk"
export NDK_HOME="$ANDROID_SDK/ndk"
export NDK_VERSION="21.4.7075529"
export ANDROID_NDK="$NDK_HOME/$NDK_VERSION"

export CMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake"
export CMAKE_MAKE_PROGRAM="$ANDROID_SDK/cmake/3.18.1/bin/ninja"

export ANDROID_API=21
export ANDROID_ARCH="arm64"
export ANDROID_ABI="arm64-v8a"

# iOS build configuration
export IOS_TOOLCHAIN_FILE="/Users/dylan/Downloads/ios-cmake/ios.toolchain.cmake"


# iOS build function
build_ios_simulator_project() {
    local project_dir=$1
    echo "building \"$project_dir\" for iOS..."

    # Create build directory
    mkdir "$project_dir/ios_simulator"
    pushd "$project_dir/ios_simulator"

    # --- 设置环境，确保使用 Xcode.app 而不是命令行工具 ---
    export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"

    # --- Configure with CMake for iOS ---
    cmake -G Xcode \
        -DCMAKE_TOOLCHAIN_FILE=/Users/dylan/Downloads/ios-cmake/ios.toolchain.cmake \
        -DPLATFORM=SIMULATORARM64 \
        -DCMAKE_XCODE_ATTRIBUTE_SDKROOT=iphonesimulator \
        -DCMAKE_XCODE_ATTRIBUTE_DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
        -DCMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang \
        -DCMAKE_CXX_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH=NO \
        -DCMAKE_XCODE_ATTRIBUTE_ARCHS="arm64" \
        -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS="iphonesimulator" \
        ..

    # --- Build and install ---
    cmake --build . --config Release --target install

    # Return to previous directory and clean up
    popd
    rm -rf "$project_dir/ios_simulator"
}

build_ios_project() {
    local project_dir=$1
    echo "building \"$project_dir\" for iOS..."

    # Create build directory
    mkdir "$project_dir/iosArm64"
    pushd "$project_dir/iosArm64"

    # --- 设置环境，确保使用 Xcode.app 而不是命令行工具 ---
    export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"

    # --- Configure with CMake for iOS ---
    cmake -G Xcode \
        -DCMAKE_TOOLCHAIN_FILE=/Users/dylan/Downloads/ios-cmake/ios.toolchain.cmake \
        -DPLATFORM=OS64 \
        -DCMAKE_BUILD_TYPE=Release \
        ..

    # --- Build and install ---
    cmake --build . --config Release --target install

    # Return to previous directory and clean up
    popd
    rm -rf "$project_dir/iosArm64"
}


if [ "$ANDROID_ARCH" = "arm" ];then
  export ANDROID_ABI="armeabi-v7a"
elif [ "$ANDROID_ARCH" = "arm64" ];then
  export ANDROID_ABI="arm64-v8a"
elif [ "$ANDROID_ARCH" = "x86" ];then
  export ANDROID_ABI="x86"
elif [ "$ANDROID_ARCH" = "x64" ];then
  export ANDROID_ABI="x86_64"
else
  export ANDROID_ABI="armeabi"
fi

working_dir=$(pwd)
echo "current working dir: $working_dir"

# android
# projects=("apollomcu" "freetype-VER-2-13-3" "nng-1.8.0" "mbedtls-3.6.5" "libtiff-4.6.0" "sqlite-3.44.2" "spdlog-2.x" "proj-9.3.1" "points2grid" "tinyxml2" "libjpeg-turbo-3.0.1" "SQLiteCpp")
# for proj in "${projects[@]}"
# do
#     echo "building \"$proj\" ..."
#     cp ./build_android_shared.sh $proj/

#     pushd $proj
#     error_code=0
#     chmod u+x build_android_shared.sh
#     ./build_android_shared.sh
#     error_code=$?
#     rm build_android_shared.sh

#     popd

#     if [ $error_code -ne 0 ]
#     then
#       exit $error_code
#     fi
# done

# ios
echo "building iOS projects..."
ios_projects=("freetype-VER-2-13-3" "nng-1.8.0" "mbedtls-3.6.5" "libtiff-4.6.0" "sqlite-3.44.2" "spdlog-2.x" "proj-9.3.1" "points2grid" "tinyxml2" "libjpeg-turbo-3.0.1" "SQLiteCpp")
for proj in "${ios_projects[@]}"
do
    build_ios_project "$proj"
    if [ $? -ne 0 ]
    then
      exit $?
    fi
done

#linux
#for proj in "${projects[@]}"
#do
#    echo "building \"$proj\" ..."
#    cp ./build_linux.sh $proj/
#
#    pushd $proj
#    error_code=0
#    chmod u+x build_linux.sh
#    ./build_linux.sh
#    error_code=$?
#    rm build_linux.sh
#    popd
#
#    if [ $error_code -ne 0 ]
#    then
#      exit $error_code
#    fi
#done


# osg android
# echo "build osg for android ..."
# error_code=0
# cp build_android_osg_shared.sh OpenSceneGraph-3.6.5/
# pushd OpenSceneGraph-3.6.5

# chmod u+x build_android_osg_shared.sh
# ./build_android_osg_shared.sh
# error_code=$?
# rm build_android_osg_shared.sh

# popd

# if [ $error_code -eq 0 ]
# then
#   echo "all done."
# else
#   echo "error occurred"
#   exit $error_code
# fi

